package com.googlecode.perfectable.visuable.objectbrowser.ui;

import com.googlecode.perfectable.eventable.routing.Sink;
import com.googlecode.perfectable.visuable.objectbrowser.CurrentObjectSelectionService;
import com.googlecode.perfectable.visuable.objectbrowser.ObjectSelectionChange;
import com.googlecode.perfectable.visuable.visual.TextualVisual;
import com.googlecode.perfectable.visuable.visual.controller.Controller;

public class CurrentObjectHeaderController implements Controller<TextualVisual> {
	private TextualVisual visual;

	private final Sink<ObjectSelectionChange> sink = new Sink<ObjectSelectionChange>() {
		@Override
		public void receive(ObjectSelectionChange event) {
			final Object currentSelection = event.getCurrentSelection();
			CurrentObjectHeaderController.this.changeText(currentSelection);
		}
	};

	@Override
	public void takeControl(@SuppressWarnings("hiding") TextualVisual visual) {
		this.visual = visual;
	}

	protected void changeText(Object currentSelection) {
		final String newText = currentSelection.getClass().getCanonicalName();
		this.visual.setText(newText);
	}

	public void setCurrentObjectSelectionService(CurrentObjectSelectionService service) {
		service.listen(this.sink);
	}
}
